import 'package:flutter/material.dart';
import 'package:net/demo/demo1.dart';
import 'package:net/demo/htext.dart';
import 'package:flutter_hooks/flutter_hooks.dart' show HookWidget;
import 'package:flutter_redux_hooks/flutter_redux_hooks.dart';
import 'package:redux/redux.dart';

// enum Actions { Add }
// int reducer(int state, action) {
//   switch (action) {
//     case Actions.Add:
//       return state + 1;
//       break;
//     default:
//       throw Error();
//   }
// }

class MyDataProvider extends StatefulWidget {
  MyDataProvider({Key key}) : super(key: key);

  @override
  _MyDataProviderState createState() => _MyDataProviderState();
}

class _MyDataProviderState extends State<MyDataProvider> {
  int state = 10;

  void add() {
    setState(() {
      state++;
    });
  }

  @override
  Widget build(BuildContext context) {
    return MyData(
      child: InheriteApp(),
      state: state,
      parent: this,
    );
  }
}

// 每次更新数据是会创建一个新的
class MyData extends InheritedWidget {
  final state;
  final _MyDataProviderState parent;
  // int state;
  MyData({key, Widget child, this.state, this.parent})
      : super(key: key, child: child);

  static getState(context) {
    //下面这段代码其实可以直接在组件中就能拿到值，这里是为了方便抽离出来的了，使用的是有调用这个静态方法就行了

    // var data = context.dependOnInheritedWidgetOfExactType<MyData>();
    // return data.state;
    return context.dependOnInheritedWidgetOfExactType<MyData>();
  }

  @override
  bool updateShouldNotify(MyData oldWidget) {
    // 是否需要渲染widget
    return oldWidget.state != state;
  }
}

main() {
  // var store = Store<int>(reducer, initialState: 8);
  // runApp(StoreProvider(
  //     store: store,
  //     child: MaterialApp(
  //       home: MyApp(),
  //     )));
  runApp(MyDataProvider());
}

class InheriteApp extends StatelessWidget {
  const InheriteApp({Key key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    var data = MyData.getState(context);
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: Text('标题')),
        body: Container(
          child: Column(
            children: [
              Text('${data.state}'),
              FlatButton(
                onPressed: () {
                  data.parent.add();
                },
                child: Text('增加'),
                color: Colors.blue,
              )
            ],
          ),
        ),
      ),
    );
  }
}

// class MyApp extends HookWidget {
//   @override
//   Widget build(BuildContext context) {
//     var a = useSelector<int, int>((state) {
//       return state;
//     });
//     final dispatch = useDispatch<int>();
//     return Scaffold(
//       appBar: AppBar(
//         title: Text('reduxDemo'),
//       ),
//       body: Container(
//         child: Row(
//           children: [
//             Text('我是$a'),
//             FlatButton(
//               onPressed: () {
//                 dispatch(Actions.Add);
//               },
//               child: Text('我要改变数字'),
//               color: Colors.blue,
//             )
//           ],
//         ),
//       ),
//     );
//   }
// }

// class MyApp extends StatefulWidget {
//   MyApp({Key key}) : super(key: key);

//   @override
//   _MyAppState createState() => _MyAppState();
// }

// class _MyAppState extends State<MyApp> {
//   @override
//   Widget build(BuildContext context) {
//     return MaterialApp(
//       title: 'Flutter Demo',
//       theme: ThemeData(
//         primarySwatch: Colors.blue,
//       ),
//       home: Scaffold(
//         appBar: AppBar(
//           title: Text('我是标题'),
//         ),
//         body: Container(
//           width: double.infinity,
//           height: double.infinity,
//           color: Colors.white,
//           child: Cmp1(),
//         ),
//       ),
//     );
//   }
// }
